package com.beanverify.inter.verifier.impl;

import java.lang.annotation.Annotation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.beanverify.annotations.Number;
import com.beanverify.exceptions.UnKnownVerifyException;
import com.beanverify.exceptions.UnSupportedParamTypeException;
import com.beanverify.inter.AnnotationVerifier;
import com.beanverify.utils.UtilCommon;

public class VerifyNumber implements AnnotationVerifier{
	private static final Logger LOG = LoggerFactory.getLogger(VerifyNumber.class);

	public boolean doVerify(Object param, String paramName, Annotation annotation) throws UnSupportedParamTypeException, UnKnownVerifyException{
		try {
			String num = (String)param;
			Number number = (Number)annotation;
			if (true == number.cantNull() && UtilCommon.isNull(num)){
				LOG.warn(String.format("[%s]参数不能为空--×",paramName));
				return false;
			}
			if (false == number.cantNull() && UtilCommon.isNull(num)){
				LOG.warn(String.format("[%s]参数为空!无法受检Number,但默认允许为空,默认可以通过--√",paramName));
				return true;
			}
			if (UtilCommon.isNumber(num)){
				LOG.debug(String.format("[%s]都是由数字组成,检查通过--√",paramName));
			} else{
				LOG.debug(String.format("[%s]中参杂了非数字,检查失败--×",paramName));
				return false;
			}
			if (num.length() > number.maxSize()){
				LOG.debug(String.format("[%s]超长,检查失败--×",paramName));
				return false;
			} else{
				LOG.debug(String.format("[%s]长度在合法范围内,检查通过--√",paramName));
				return true;
			}
		} catch(ClassCastException e){
			LOG.error("VerifyNumber参数检查异常",e);
			throw new UnSupportedParamTypeException("VerifyNumber参数检查异常");
		} catch(Exception e){
			LOG.error("VerifyNumber参数检查异常",e);
			throw new UnKnownVerifyException("未知的VerifyNumber异常!");
		}
	}

}